package cc.deng.wubo.minapp.controller;

import cc.deng.wubo.minapp.beans.FileUploadInfo;
import cc.deng.wubo.minapp.core.base.Result;
import cc.deng.wubo.minapp.service.FileUploadService;
import cc.deng.wubo.minapp.utils.StringUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nutz.dao.Dao;
import org.nutz.filepool.NutFilePool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/file")
@SuppressWarnings("all")
public class FileUploadController {
    private final Log logger = LogFactory.getLog(this.getClass());
    @Autowired
    private NutFilePool tempFilePool;
    @Autowired
    private FileUploadService fileUploadService;
    @Autowired
    private Dao dao;




    @RequestMapping("/test")
    @ResponseBody
    public Object test(){
        return Result.success("Hello Buddy! 壮哥你好~");
    }


    @RequestMapping("/pageShowFile")
    @ResponseBody
    public void pageShowFile(HttpServletRequest request, HttpServletResponse response, Long fid){
        InputStream in = null;
        OutputStream out = null;
        try{
            FileUploadInfo fileUploadInfo = dao.fetch(FileUploadInfo.class,fid);
            if (fileUploadInfo!=null){
                response.reset();
                response.setHeader("Content-Disposition", "");
                response.setHeader("Pragma", "No-cache");
                response.setHeader("Cache-Control", "No-cache");
                response.setDateHeader("Expires", 0);
                // 创建读取文件的流
                in = new FileInputStream(fileUploadInfo.getServerPath());
                // 创建输出文件的流，也就是response的OutputStream
                out = response.getOutputStream();

                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = in.read(buffer))>0){
                    out.write(buffer,0,len);
                }
                out.flush();
            }

        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }finally {
            try{
                if (in!=null)
                    in.close();
                if (out!=null)
                    out.close();
            }catch (Exception e){

            }
        }
    }


    @RequestMapping("/upload")
    @ResponseBody
    public Object upload( MultipartFile[] file){

        List<String> results = new ArrayList<>();
        try{
            for (MultipartFile ft:file){
                byte[] bytes = ft.getBytes();
                String suffix = StringUtil.suffix(ft);
                File tf = tempFilePool.createFile(suffix);
                OutputStream out = new FileOutputStream(tf);
                out.write(bytes);
                out.flush();
                out.close();
                System.out.println(tf.getPath());
                results.add(tf.getPath());
            }
            return Result.success("fileupload.success", results);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }

    @RequestMapping("/uploadTest")
    @ResponseBody
    public Object uploadTest( MultipartFile[] file){

        List<String> results = new ArrayList<>();
        try{
            for (MultipartFile ft:file){
                byte[] bytes = ft.getBytes();
                String suffix = StringUtil.suffix(ft);
                File tf = tempFilePool.createFile(suffix);
                OutputStream out = new FileOutputStream(tf);
                out.write(bytes);
                out.flush();
                out.close();
                System.out.println(tf.getPath());
                results.add(tf.getPath());
            }
            List<FileUploadInfo> upload = fileUploadService.upload(results);
            return Result.success("success",upload);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }


}
